
import java.util.Arrays;

//数组
public class Test {
    public static void main(String[] args) {
        int[] array = new int[] {1,2,3,4,5};
        int[] array10 = {3,6,1,7,2,4,5};
        //数组转字符串
        String ret = Arrays.toString(array);
        System.out.println("数组转字符串" + ret);
        //自己实现 toString 的功能
        String ret1 = myToString(array);
        System.out.println("自己实现数组转字符串" + ret1);
        //数组的拷贝
        int[] array1 = Arrays.copyOf(array,array.length);
        System.out.println("数组的拷贝" + myToString(array1));
        //自己实现数组的拷贝
        int[] array2 = copyArray(array);
        System.out.println("自己实现数组的拷贝" + myToString(array2));
        //另一种数组的拷贝，可实现局部拷贝
        int[] array3 = new int[array.length];
        System.arraycopy(array,0,array3,0,array.length);
        System.out.println("另一种数组的拷贝（可实现局部拷贝）" + myToString(array3));
        //另一种局部拷贝
        int[] array4 = Arrays.copyOfRange(array,2,4);//左闭右开区间，故只有3，4
        System.out.println("另一种局部数组的拷贝" + Arrays.toString(array4));
        //不常用拷贝
        int[] array5 = array.clone();
        System.out.println("不常用拷贝" + Arrays.toString(array5));
        //排序 底层是快排
        Arrays.sort(array);
        //自带的二分查找
        System.out.println("自带的二分查找结果：" + Arrays.binarySearch(array, 2));
        //自己写二分查找
        System.out.println("自己写的二分查找结果：" + binarySreach(array, 1));
        //自己写的数组逆序
        reverse(array);
        System.out.println("逆序的结果：" + Arrays.toString(array));
        //冒泡排序
        bubbleSort(array10);
        System.out.println("冒泡结果：" + Arrays.toString(array10));
        //二维数组
        int[][] arrays = new int[2][3];
        //for循环打印
        arraysForPrintf(arrays);
        System.out.println();
        //for each 打印
        arraysForEachPrintf(arrays);
        System.out.println();
        //自带的二维数组打印 只能打在一行上
        System.out.println(Arrays.deepToString(arrays));
        System.out.println();
        //不规则数组
        int[][] arrays1 = new int[2][];
        arrays1[0] = new int[3];
        arrays1[1] = new int[5];
        arraysForPrintf(arrays1);
        System.out.println();
        System.out.println(Arrays.deepToString(arrays1));
    }

    //数组转字符串
    public static String myToString(int[] array){
        if(array == null){
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            if(i == array.length - 1){
                ret = ret + array[i] + "]";
                break;
            }
            ret = ret + array[i] + ", ";
        }
        return ret;
    }

    //数组的拷贝
    public static int[] copyArray(int[] array){
        int[] array2 = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }
        return array2;
    }

    //数组的二分查找
    public static int binarySreach(int[] array,int key){
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(array[mid] == key){
                return mid;
            }else if(array[mid] < key){
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }

    //数组逆序
    public static void reverse(int[] array){
        int left = 0;
        int right = array.length - 1;
        int tmp = 0;
        while(left < right) {
            tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            //优化 检测有序
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }

    //for循环打印二维数组
    public static void arraysForPrintf(int[][] arrays){
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[i].length; j++) {
                System.out.print(arrays[i][j] + " ");
            }
            System.out.println();
        }
    }

    //for each 打印二维数组
    public static void arraysForEachPrintf(int[][] arrays){
        for (int[] array:arrays) {
            for (int x:array) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }

}
